package com.example.boot.express.base;

import com.ql.util.express.ExpressRunner;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.function.Consumer;

@Component
public class QlExpressUtil implements ApplicationContextAware {

    private ExpressRunner runner;

    public QlExpressUtil() {
        runner = new ExpressRunner();
    }

    public void initRunner(Consumer<ExpressRunner> consumer) {
        consumer.accept(runner);
    }

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public Object execute(String statement, Map<String, Object> context) throws Exception {
        QLExpressContext expressContext = new QLExpressContext(context, applicationContext);
        return runner.execute(statement, expressContext, null, false, false);
    }

}
